<?php

namespace Home\Controller;

class AccountController extends BaseMallController {

    function _initialize() {
        parent::_initialize();
        $this->_user_id = I('request.user_id');
    }

    //对账单列表
    function get_list(){
        if(!$_POST['page'] || !$_POST['pagenum']){
            render('10004', '参数错误');
        }
        $cond['user_id'] = $this->_user_id;
//        if(I('request.start_time') && I('request.end_time')){
//            $start_time = strtotime(I('request.start_time').'-01 00:00:00');
//            $end_time_first = strtotime(I('request.end_time').'-01 00:00:00');
//            $end_time = strtotime(date('Y-m-d 23:59:59', strtotime(date('Y-m-d H:i:s',$end_time_first).' +1 month -1 day')));
//            $cond['ecm_commission.finish_time'] = array(
//                'between',array($start_time,$end_time)
//            );
//        }else{
//            $start_time = strtotime(date('Y-m-d 00:00:00').' -7 day');
//            $end_time = time();
//            $cond['ecm_commission.finish_time'] = array(
//                'between',array($start_time,$end_time)
//            );
//        }
        $res = M('commission_account')
            ->field('ecm_commission_account.*,FROM_UNIXTIME(end_time,"%Y") as year')
            ->where($cond)
            ->order('end_time desc')
            ->limit($_POST['pagenum'])
            ->page($_POST['page'])
            ->select();
        $res_year = array();
        foreach ($res  as $k => $v){
            $res[$k]['start_time'] = date("m.d",$v['start_time']);
            $res[$k]['end_time'] = date("m.d",$v['end_time']);
            switch($v['status']) {
                case 1:
                    $res[$k]['status'] = '待审核';
                    break;
                case 2:
                    $res[$k]['status'] = '待付款';
                    break;
                case 3:
                    $res[$k]['status'] = '已付款';
                    break;
            }
            $res_year[$v['year']]['year'] = $v['year'];
            $res_year[$v['year']]['data'][] = $res[$k];

        }
        render('10000','查询成功',array('data'=>$res_year ? array_values($res_year) : array()));
    }

    //对账单明细列表
    function get_detail_list()
    {
        if (!$_POST['page'] || !$_POST['pagenum'] || !$_POST['account_id']) {
            render('10004', '参数错误');
        }
        $cond['user_id'] = $this->_user_id;
//        $cond['start_time'] = strtotime($_POST['year'].'.'.$_POST['start_time']);
//        $cond['end_time'] = strtotime($_POST['year'].'.'.$_POST['end_time']);
//        if($cond['start_time'] > $cond['end_time']){
//            $cond['start_time'] = strtotime($_POST['year'].'.'.$_POST['start_time'].' -1 year');
//        }
        $cond['id'] = $_POST['account_id'];
        if(I('request.order')){
            $order = $_POST['order'];
        }else{
            $order = 'desc';
        }
//        $res_detail = M('commission_detail')->where()->select();

//        if(I('request.start_time') && I('request.end_time')){
//            $start_time = strtotime(I('request.start_time').'-01 00:00:00');
//            $end_time_first = strtotime(I('request.end_time').'-01 00:00:00');
//            $end_time = strtotime(date('Y-m-d 23:59:59', strtotime(date('Y-m-d H:i:s',$end_time_first).' +1 month -1 day')));
//            $cond['ecm_commission_log.finish_time'] = array(
//              'between',array($start_time,$end_time)
//            );
//        }else{
//            $start_time = strtotime(date('Y-m-d 00:00:00').' -7 day');
//            $end_time = time();
//            $cond['ecm_commission_log.finish_time'] = array(
//                'between',array($start_time,$end_time)
//            );
//        }
        $res = M('commission_detail')
            ->where($cond)
            ->order('order_finish_time '.$order)
            ->limit($_POST['pagenum'])
            ->page($_POST['page'])
            ->select();


        foreach ($res as $k => $v) {
            $res[$k]['order_pay_time'] = date("Y.m.d H:i", $v['order_finish_time']);
            $res[$k]['order_finish_time'] = date("Y.m.d H:i", $v['order_finish_time']);
            $v['user_name'] ? $res[$k]['user_name'] = substr_replace($v['user_name'], '****', 3, 4) : '';
        }
        render('10000','查询成功',array('data'=>$res ? $res : array()));
    }




}
